﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Reflection;

namespace UI.Core
{
    public class Utility
    {
        public static bool IsDesignMode()
        {

            bool returnFlag = false;



            if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
            {

                returnFlag = true;

            }

            else if (Process.GetCurrentProcess().ProcessName == "devenv")
            {

                returnFlag = true;

            }



            return returnFlag;

        }

       public static IEnumerable<FieldInfo> GetAllControl(Type type, FormBase form)
        {
            if (IsDesignMode())
                return new List<FieldInfo>();
            List<FieldInfo> flist = new List<FieldInfo>();
            Type t = type;
            while (true)
            {
                var fs = t.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (var f in fs)
                {
                    if (f.Name.EndsWith("BackingField")) 
                    {
                        continue;
                    }
                    if (flist.Where(item => item.Name == f.Name).Count() == 0)
                    {
                        
                        flist.Add(f);
                    }
                }
                t = t.BaseType;
                if (t == typeof(FormBase))
                    break;
             
            }
            return flist;
        }
    }
}
